package com.mcxx.modules.minimumliving.controller;

import com.alibaba.fastjson.JSON;
import com.mcxx.modules.minimumliving.service.SpecialSignManagementService;
import com.mcxx.modules.miniSalvation.special.dto.SpecialSignDTO;
import com.mcxx.modules.miniSalvation.special.entity.SpecialSignEntity;
import com.mcxx.modules.miniSalvation.special.param.SpecialSignParam;
import com.mcxx.modules.miniSalvation.special.service.read.SpecialSignReadService;
import com.mcxx.modules.miniSalvation.special.service.write.MemberSpecialSignWriteService;
import com.mcxx.modules.miniSalvation.special.service.write.SpecialSignWriteService;
import com.mcxx.result.BaseResult;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;


@Validated
@Api("暂停管理")
@RestService({"minimumliving/special/v2"})
public class SpecialSignManagementController extends DefaultRestService {
    private static final Logger logger = LoggerFactory.getLogger(SpecialSignManagementController.class);
    @Autowired
    private SpecialSignManagementService specialSignManagementService;
    @Autowired
    private SpecialSignWriteService specialSignWriteService;
    @Autowired
    private SpecialSignReadService specialSignReadService;
    @Autowired
    private MemberSpecialSignWriteService memberSpecialSignWriteService;


    @PostMapping(value = "querySpecialList")
    @ApiOperation(value = "标识类型配置查询")
    public void querySpecialList(SpecialSignParam param, PageParam pageParam) {
        logger.info("标识类型配置查询,条件是{}", param);
        Page page = specialSignManagementService.querySpecialList(param, getCurUserAccount(), pageParam);
        this.setPageBean(page);
        logger.info("标识类型配置查询,结果是{}", page);
    }
    @PostMapping("addSpecialSign")
    @ApiOperation("新增特殊标识")
    public void addSpecialSign(SpecialSignDTO dto){
        logger.info("新增特殊标识，参数是：{}", dto);
        int rstFlag = specialSignManagementService.addSpecialSign(dto,getCurUserAccount());
        setData(true);
        logger.info("新增特殊标识，结果是：{}", rstFlag);
    }

    @PostMapping("deleteSpecialSignById")
    @ApiOperation("删除特殊标识信息")
    public void deleteSpecialSignById(@V({"notnull"}) String id ,@V({"notnull"}) Integer specialValue){
        logger.info("删除特殊标识信息，参数是：{}", id);
        BaseResult<Boolean> rstMsg  = new BaseResult<>();


        memberSpecialSignWriteService.deleteBySpecialValue(specialValue.toString());
        logger.info("删除原有记录标识信息，参数是：{}", specialValue.toString());
        int rst = specialSignWriteService.deleteById(id);
        if(rst <= 0){
            rstMsg.setData(false);
            rstMsg.setMsg("操作异常，删除失败");
        }
        rstMsg.setData(true);
        rstMsg.setMsg("删除成功");
        setData("data", rstMsg);
        logger.info("新增特殊标识，结果是：{}", rstMsg);
    }

    @PostMapping("updateSpecialSignById")
    @ApiOperation("修改特殊标识数据")
    public void updateSpecialSignById(SpecialSignDTO dto){
        logger.info("修改特殊标识数据，参数是：{}", dto);
        BaseResult<Boolean> rstMsg  = new BaseResult<>();
        if (CommonUtil.invalidArgs(dto.getId())) {
            setError(FailureCode.ERR_10000.getMessage());
            return;
        }
        specialSignManagementService.updateSpecialSignById(dto,getCurUserAccount());
        rstMsg.setData(true);
        rstMsg.setMsg("操作成功");
        setData("data", rstMsg);
        logger.info("修改特殊标识数据完成", true);
    }


    @PostMapping("getSalvationObjectList")
    @ApiOperation("查询供养对象列表")
    public void getSalvationObjectList(SpecialSignParam param, PageParam pageParam){
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        logger.info("查询供养对象列表,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = specialSignReadService.getSalvationObjectList(param,pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询供养对象列表完成，结果：{}",JSON.toJSONString(page));
    }


    @GetMapping("getSpecialSign")
    @ApiOperation("获取特殊标识数据")
    public void getSpecialSign(){
        logger.info("获取特殊标识数据");
        List<SpecialSignEntity> SpecialSignList = specialSignReadService.getSpecialSign(getCurUserAccount());
        setData(SpecialSignList);
        logger.info("获取特殊标识数据,结果是：{}", SpecialSignList);
    }



    @PostMapping("addMemberSpecialSign")
    @ApiOperation("保存成员特殊标识")
    public void addMemberSpecialSign(SpecialSignDTO dto){
        logger.info("保存成员特殊标识，参数是：{}", dto);
        specialSignManagementService.addMemberSpecialSign(dto,getCurUserAccount());
        setData(true);
        logger.info("保存成员特殊标识，结果是：{}", true);
    }


    @PostMapping(value = "getAuditSpecialSignList")
    @ApiOperation(value = "特殊标识初审列表查询")
    public void getAuditSpecialSignList(SpecialSignParam param, PageParam pageParam) {
        logger.info("特殊标识初审列表查询,条件是{}", param);
        Page page = specialSignManagementService.getAuditSpecialSignList(param, getCurUserAccount(), pageParam);
        this.setPageBean(page);
        logger.info("特殊标识初审列表查询,结果是{}", page);
    }


    @PostMapping("updateMemberSpecialSign")
    @ApiOperation("编辑成员特殊标识")
    public void updateMemberSpecialSign(SpecialSignDTO dto){
        logger.info("编辑成员特殊标识，参数是：{}", dto);
        specialSignManagementService.updateMemberSpecialSign(dto,getCurUserAccount());
        setData(true);
        logger.info("编辑成员特殊标识，结果是：{}", true);
    }

    @PostMapping("deleteSpecialSignByIdcard")
    @ApiOperation("删除成员特殊标识信息")
    public void deleteSpecialSignByIdcard(@V({"notnull"}) String idcard ,@V({"notnull"}) String memberStatus){
        logger.info("删除成员特殊标识信息，参数是：{}", idcard);
        BaseResult<Boolean> rstMsg  = new BaseResult<>();

        int rst = specialSignManagementService.deleteSpecialSignByIdcard(idcard,memberStatus,getCurUserAccount());
        if(rst <= 0){
            rstMsg.setData(false);
            rstMsg.setMsg("操作异常，删除失败");
        }
        rstMsg.setData(true);
        rstMsg.setMsg("删除成功");
        setData("data", rstMsg);
        logger.info("删除成员特殊标识信息，结果是：{}", rstMsg);
    }

    @PostMapping("auditSpecialSignByIdcard")
    @ApiOperation("成员特殊标识提交区县审核")
    public void auditSpecialSignByIdcard(@V({"notnull"}) String idcard ,@V({"notnull"}) String memberStatus){
        logger.info("成员特殊标识提交区县审核，参数是：{}", idcard);
        BaseResult<Boolean> rstMsg  = new BaseResult<>();

        int rst = specialSignManagementService.auditSpecialSignByIdcard(idcard,memberStatus,getCurUserAccount());
        if(rst <= 0){
            rstMsg.setData(false);
            rstMsg.setMsg("操作异常，提交失败");
        }
        rstMsg.setData(true);
        rstMsg.setMsg("提交成功");
        setData("data", rstMsg);
        logger.info("成员特殊标识提交区县审核，结果是：{}", rstMsg);
    }


    @PostMapping(value = "getConfirmSpecialSignList")
    @ApiOperation(value = "特殊标识审核列表")
    public void getConfirmSpecialSignList(SpecialSignParam param, PageParam pageParam) {
        logger.info("特殊标识审核列表,条件是{}", param);
        Page page = specialSignManagementService.getConfirmSpecialSignList(param, getCurUserAccount(), pageParam);
        this.setPageBean(page);
        logger.info("特殊标识审核列表,结果是{}", page);
    }


    @PostMapping("confirmMemberSpecialSign")
    @ApiOperation("审核成员特殊标识")
    public void confirmMemberSpecialSign(SpecialSignDTO dto){
        logger.info("审核成员特殊标识，参数是：{}", dto);
        specialSignManagementService.confirmMemberSpecialSign(dto,getCurUserAccount());
        setData(true);
        logger.info("审核成员特殊标识，结果是：{}", true);
    }


    @PostMapping("getMemberSpecialSignList")
    @ApiOperation("查询特殊标识对象列表")
    public void getMemberSpecialSignList(SpecialSignParam param, PageParam pageParam){
        param.setBusinessType(Constant.BusinessCode.DIBAO);
        logger.info("查询特殊标识对象列表,查询参数：{},分页参数：{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = specialSignReadService.getMemberSpecialSignList(param,pageParam,getCurUserAccount());
        setPageBean(page);
        logger.info("查询特殊标识对象列表，结果：{}",JSON.toJSONString(page));
    }


    @PostMapping("changeMemberSpecialSign")
    @ApiOperation("编辑成员特殊标识")
    public void changeMemberSpecialSign(SpecialSignDTO dto){
        logger.info("编辑成员特殊标识，参数是：{}", dto);
        specialSignManagementService.changeMemberSpecialSign(dto,getCurUserAccount());
        setData(true);
        logger.info("编辑成员特殊标识，结果是：{}", true);
    }

}
